package com.ruoyi.web.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ItemNotFoundException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.course.domain.SysCourse;
import com.ruoyi.course.service.ISysCourseService;
import com.ruoyi.exam.serive.IScoresService;
import com.ruoyi.exam.vo.ScoresCombineVO;
import com.ruoyi.exam.vo.ScoresCourseVO;
import com.ruoyi.questionBank.domain.ExamPaper;
import com.ruoyi.questionBank.service.IExamPaperService;
import com.ruoyi.student.impl.StudentUserService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.student.vo.UserInfoVO;
import com.ruoyi.web.api.vo.StudyProcessVO;
import com.ruoyi.web.controller.scores.ScoresController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/api/user")
public class UserApi extends BaseController {
    @Autowired
    private StudentUserService userService;
    @Autowired
    private ScoresController scoresController;
    @Autowired
    private IExamPaperService paperService;
    @Autowired
    private ISysCourseService courseService;
    @Autowired
    private IScoresService scoresService;

    @GetMapping("/info")
    public AjaxResult getUserInfo(){
        LoginUser loginUser = getLoginUser();
        long uid = loginUser.getUserId();
        UserInfoVO userInfoVO = userService.getUserInfo(uid);
        return AjaxResult.success(userInfoVO);
    }

    //综合成绩
    @GetMapping("/scores")
    public AjaxResult scores(){
        LoginUser loginUser = getLoginUser();
        long uid = loginUser.getUserId();
        //
//        ExamPaper examPaper = paperService.getTheoryProcess();
        //
        ScoresCombineVO scoresCombineVO = new ScoresCombineVO();
        scoresCombineVO.setUserId(uid);
//        scoresCombineVO.setPaperId(examPaper == null ? 0L : examPaper.getId());
        scoresCombineVO.setPaperId(1L); //写死试卷ID是1
        TableDataInfo dataInfo = scoresController.combineScores(scoresCombineVO);
        if (!CollectionUtils.isEmpty(dataInfo.getRows())) {
            return AjaxResult.success(dataInfo.getRows().get(0));
        }
        //
        scoresCombineVO.setNickName(loginUser.getUsername());
        scoresCombineVO.setUserName(loginUser.getUsername());
        UserInfoVO userInfoVO = userService.getUserInfo(uid);
        if (userInfoVO != null) {
            scoresCombineVO.setNickName(userInfoVO.getNickname());
            scoresCombineVO.setUserName(userInfoVO.getUserName());
        }
        return AjaxResult.success(scoresCombineVO);
    }

    //学习进度
    @GetMapping("/process")
    public AjaxResult process() {
        StudyProcessVO processVO = new StudyProcessVO();
        processVO.setExampleList(new ArrayList<>());
        processVO.setOperatorList(new ArrayList<>());
        processVO.setWartimeList(new ArrayList<>());
        processVO.setProcess("0");
        //用户
        LoginUser loginUser = getLoginUser();
        long uid = loginUser.getUserId();
        UserInfoVO userInfoVO = userService.getUserInfo(uid);
        processVO.setUserInfo(userInfoVO);
        //课程
        SysCourse course = new SysCourse();
        course.setDelFlag("0");
        course.setReleaseStatus(0);
        List<SysCourse> courseList = courseService.selectSysCourseList(course);
        if (CollectionUtils.isEmpty(courseList)) {
            return AjaxResult.success(processVO);
        }
        //学习进度
        ScoresCourseVO scoresCourseVO = new ScoresCourseVO();
        scoresCourseVO.setDelFlag(0);
        scoresCourseVO.setReleaseStatus(0);
        scoresCourseVO.setUserId(uid);
        List<ScoresCourseVO> scoresList = scoresService.selectScoresCourseList(scoresCourseVO);
        //成绩超过60分的
        Long sucessCount = 0L;
        Map<Long,ScoresCourseVO> scoresMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(scoresList)) {
            for (ScoresCourseVO courseVO :scoresList ) {
                scoresMap.put(courseVO.getCourseNo(),courseVO);
                if (courseVO.getScores() != null && courseVO.getScores() >= 60) {
                    sucessCount++;
                }
            }
        }
        courseList.forEach(item -> {
            if (item.getCourseType() == 1) {    //原理示教
                processVO.getExampleList().add(findScoresCourseVO(item,scoresMap));
            }

            if (item.getCourseType() == 2) {    //操作体验
                processVO.getOperatorList().add(findScoresCourseVO(item,scoresMap));
            }

            if (item.getCourseType() == 3) {    //战时运用
                processVO.getWartimeList().add(findScoresCourseVO(item,scoresMap));
            }
        });
        //
        processVO.setProcess(StringUtils.getPercentage(sucessCount,(long)courseList.size()));
        //
        return AjaxResult.success(processVO);
    }

    private ScoresCourseVO findScoresCourseVO(SysCourse course,Map<Long,ScoresCourseVO> scoresMap) {
        ScoresCourseVO vo = scoresMap.get(course.getCourseNo());
        if (vo == null) {
            vo = new ScoresCourseVO();
            vo.setCourseId(course.getCourseId());
            vo.setCourseName(course.getCourseName());
            vo.setCourseNo(course.getCourseNo());
            vo.setStudySchedule("0%");
            vo.setStudyStatus(1); //未开始
            vo.setCourseType(course.getCourseType());
            vo.setScores(0L);
        }
        return vo;
    }

}


